package com.zxn.service;

import com.zxn.utils.ElasticUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;

/**
 * program: springbootelastic
 * description:聚合操作，sum max
 * author: zxn
 * create: 2019-04-06 22:36
 **/
public class AggregationOperationService {

    /**
     * 直接求和的方法
     */
    public void  QuerySum(){
        TransportClient client = ElasticUtils.getClient();
        AggregationBuilder agg = AggregationBuilders.sum("sum_count").field("count");
        SearchResponse response = client.prepareSearch("agg_test").setTypes("test")
                                          .addAggregation(agg)
                                          .get();
        Sum aggs = response.getAggregations().get("sum_count");
        System.out.println("汇总数据:" + aggs.getValueAsString());
    }

    /**
     * 先分类在求和方法
     */
    public void  queryClassifySum(){
        TransportClient client = ElasticUtils.getClient();
        AggregationBuilder agg = AggregationBuilders.terms("agg_person").field("name");
        agg.subAggregation(AggregationBuilders.sum("sum_count").field("count"));
        SearchResponse response = client.prepareSearch("agg_test").setTypes("test")
                .addAggregation(agg)
                .get();
        Terms aggs = response.getAggregations().get("agg_person");
        for (Terms.Bucket bucket : aggs.getBuckets()) {
            System.out.print("汇总数据姓名:" + bucket.getKeyAsString());
            Sum data = bucket.getAggregations().get("sum_count");
            System.out.println("汇总数据:" +data.getValueAsString());
        }
    }

    /**
     * 分类之后求和求平均值
     */
    public void queryMutilData(){
        TransportClient client = ElasticUtils.getClient();
        AggregationBuilder agg = AggregationBuilders.terms("agg_person").field("name");
        agg.subAggregation(AggregationBuilders.sum("sum_count").field("count"));
        agg.subAggregation(AggregationBuilders.avg("avg_count").field("count"));
        SearchResponse response = client.prepareSearch("agg_test").setTypes("test")
                .addAggregation(agg)
                .get();
        Terms aggs = response.getAggregations().get("agg_person");
        for (Terms.Bucket bucket : aggs.getBuckets()) {
            System.out.print("汇总数据姓名:" + bucket.getKeyAsString());
            Sum data = bucket.getAggregations().get("sum_count");
            Avg avg = bucket.getAggregations().get("avg_count");
            System.out.print("平均值:" +avg.getValueAsString());
            System.out.println("汇总数据:" +data.getValueAsString());
        }
    }


    public static void main(String[] args) {
       // 把上述方法拷贝到这里直接测试使用
    }
}
